﻿using MengLin.OOP.DesignPattern.IteratorPattern.BuyTicket.Abstract;
using MengLin.OOP.DesignPattern.IteratorPattern.BuyTicket.Implements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.IteratorPattern.BuyTicket
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建聚集对象
            ConcreteAggregate concreteAggregate = new ConcreteAggregate();
            concreteAggregate[0] = "张三";
            concreteAggregate[1] = "李四";
            concreteAggregate[2] = "王五";
            concreteAggregate[3] = "行李";
            concreteAggregate[4] = "老外";
            concreteAggregate[5] = "公交公司内部员工";

            Console.WriteLine("===============迭代器模式正序买票===================");
            {
                //创建迭代器对象
                AbstractIterator iterator = new ConcreteIterator(concreteAggregate);
                object firstObj = iterator.First();
                object lastObj = iterator.Last();
                while (!iterator.IsDone())
                {
                    Console.WriteLine($"{iterator.CurretItem()}请买票.");
                    iterator.Next();//下一个
                }
            }
            Console.WriteLine("===============迭代器模式倒序买票===================");
            {
                //创建倒序迭代器对象
                AbstractIterator iteratorDesc = new ConcreteIteratorDesc(concreteAggregate);
                object firstObj = iteratorDesc.First();
                object lastObj = iteratorDesc.Last();
                while (!iteratorDesc.IsDone())
                {
                    Console.WriteLine($"{iteratorDesc.CurretItem()}请买票.");
                    iteratorDesc.Next();//下一个
                }
            }

            #region .Net的迭代器实现
            Console.WriteLine("===============.Net的迭代器实现买票===================");
            {
                IList<string> objList = new List<string>();
                objList.Add("张三");
                objList.Add("李四");
                objList.Add("王五");
                objList.Add("行李");
                objList.Add("老外");
                objList.Add("公交公司内部员工");
                //foreach是实现了IEnumerable，IEnumerator这两个接口来实现循环遍历
                foreach (var item in objList)
                {
                    Console.WriteLine($"{item}请买票.");
                }

                Console.WriteLine("===============IEnumerable,IEnumerator实现买票===================");
                IEnumerator<string> e = objList.GetEnumerator();
                while(e.MoveNext())
                {
                    Console.WriteLine($"{e.Current}请买票.");
                }
            }
            #endregion

            Console.Read();
        }
    }
}
